<?php

namespace Common\Model;

use Think\Model\RelationModel;

class VUserTaskQuotaModel extends RelationModel
{
    protected $_link = array(
        'user_task_annex' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'UserTaskAnnex',
            'foreign_key' => 'user_task_quota_id',
        ),
    );

    public function get($id)
    {
        $result = $this->where('id="%d"', $id)->find();
        return $result;
    }

    public function read($id)
    {
        $data['status'] = 1;
        $result = $this->where('id=%d', $id)->save($data);
        if ($result) {
            return true;
        }
        return false;
    }

    public function get_list($user_task_id, $status)
    {
        $condition = $this->assembleCondition($user_task_id, $status);

        if ($page_size > 0) {
            $list = $this->relation(true)->where($condition)->order('id asc')->select();
        } else {
            $list = $this->relation(true)->where($condition)->order('id asc')->select();
        }
        return $list;
    }

    private function assembleCondition($user_task_id, $status, $task_type)
    {
        $condition = array();
        if (!empty($user_task_id)) {
            $condition['user_task_id'] = array('eq', $user_task_id);
        }

        if ($status != "" && $status != null) {
            if (is_array($status)) {
                $condition['status'] = array('in', $status);
            } else {
                $condition['status'] = array('eq', $status);
            }
        } else {
            $condition['status'] = array('egt', 0);
        }

        return $condition;
    }
}